package UILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import java.util.ArrayList;
import CTRLayer.PersonCTR;//The TUILayer needs to know the ControlLayer
import ModelLayer.*;

/**
 * This class allows the user to interact with the IT-system. In this class
 * the user can create persons and find persons that already exists.
 * 
 * @author Gruppe 4 
 * @version 4. juni 2012
 */
public class PersonUI
{   
    private PersonCTR personCTR;//reference to the ControlLayer
    
    /**
     * The constructor of the class initializes "PersonCTR", which is the class
     * that controls the person use case.
     */
    public PersonUI()
    {
       personCTR = new PersonCTR();
    }
    
    /**
     * This method starts the person menu.
     */
    public void start()
    {
       personMenu();
    }
    
    /**
     * This method calls a method according to what the user wishes.
     */
    private void personMenu()
    {
        boolean exit = false;
        while (!exit)
        {
            int choice = writePersonMenu();
            switch (choice)
            {
                case 1:
                    startGetPerson();
                    break;  
                case 2:
                    startAddPerson();
                    break;
                case 3:
                    startDeletePerson();
                    break;
                case 4:
                    StartUpdatePersonMenu();
                    break;
                case 5:
                    startListAllPersons();
                    break;
                case 6:
                    exit = true;
                    break;
            }
        }
    }
    
    /**
     * This method is the menu that the user sees when he/she enters the
     * part of the program that controls persons.
     */
    private int writePersonMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Person Menu ***");
        System.out.println(" (1) Show information about person");
        System.out.println(" (2) Add person");
        System.out.println(" (3) Delete person");
        System.out.println(" (4) Update person");
        System.out.println(" (5) List all persons");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");
        
        int choise = keyboard.nextInt();
        return choise;
    }
    
    /**
     * The method displays the Update Person Menu.
     */
    private void StartUpdatePersonMenu()
    {      // makes an object keyboard to read input from the screen
        String phone = inputPhone();
        Person person = null;
        person = personCTR.findPerson(phone);
        
        try {
            if (person != null) {
                printPersonType(person);
                printPersonGeneral(person);
                System.out.println("\n\n");
            } else {
                System.out.println("\n ** No person with that phone number was found! **");
                Thread.sleep(1500);
            }
        } catch (Exception e) {
            StartUpdatePersonMenu();
        }
        
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Update Person Menu ***");
        System.out.println(" (1) Update Person Name");
        System.out.println(" (2) Update Person Address");
        System.out.println(" (3) Update Person City");
        System.out.println(" (4) Update Person Postalcode");
        System.out.println(" (5) Update Person PhoneNo");
        System.out.println(" (6) Update Person Email");
        System.out.println(" (7) Update Person Account Balance");
        System.out.print("\n Make your choice: ");
        
        int choice = keyboard.nextInt();
        makeUpdate(person, choice);
    }
    
    /**
     * The method controls update procedure.
     */
    private void makeUpdate(Person person, int choice)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n\nWrite new person data:  ");
        int newInt = 0;
        String newString = "";
        double newDouble = 0.0;
        try {
            newInt = keyboard.nextInt();
        } catch (Exception e) {
            try {
                newDouble = keyboard.nextDouble();
            } catch (Exception ex) {
                newString = keyboard.nextLine();
            }
        } 
        
        if (choice == 1) {
            person.setName(newString);
        } else if (choice == 2) {
            person.setAddress(newString);
        } else if (choice == 3) {
            person.setCity(newString);
        } else if (choice == 4) {
            person.setPostalCode(newInt);
        } else if (choice == 5) {
            person.setPhoneNo(newString);
        } else if (choice == 6) {
            person.setEmail(newString);
        } else if (choice == 7) {
            Customer customer = (Customer) person;
            newDouble = customer.getAccountBalance() + newDouble;
            customer.setAccountBalance(newDouble);
        }
        
    }
    
    // start input methods
    /**
     * The method takes the input of name of person.
     */
    private String inputName()
    {
        boolean ok = false;
        String name = "";
        
        while (!ok) {
            try {
                // makes an object keyboard to have input from the screen
                Scanner keyboard = new Scanner(System.in);
                System.out.print(" Write Name:  ");
                name = keyboard.nextLine();
                ok = true;
            } catch (Exception e) {
                System.out.println("An error has occurred");
            }
        }
         
         return name;
    }
    
    /**
     * The method takes the input of address of person.
     */
    private String inputAddress()
    {
        boolean ok = false;
        String address = "";
        
        while (!ok) {
            try {
                // makes an object keyboard to read input from the console
                Scanner keyboard = new Scanner(System.in);
                System.out.print(" Write Address: ");
                address = keyboard.nextLine();
                ok = true;
            } catch (Exception e) {
                System.out.println("An error has occurred");
            }
        }
                
        return address;
    }
    
    /**
     * The method takes the input of city of person.
     */
    private String inputCity()
    {
        boolean ok = false;
        String city = "";
        
        while (!ok) {
            try {
                // makes an object keyboard to have input from the screen
                Scanner keyboard = new Scanner(System.in);
                System.out.print(" Write City:  ");
                city = keyboard.nextLine();
                ok = true;
            } catch (Exception e) {
                System.out.println("An error has occurred");
            }
        }
        
        return city;
    }
    
    /**
     * The method takes the input of postal code of person.
     */
    private int inputPostalCode()
    {
        boolean ok = false;
        int postalCode = 0;
        
        while (!ok) {
            try {
                // makes an object keyboard to read input from the console
                Scanner keyboard = new Scanner(System.in);
                System.out.print(" Write Postal Code:  ");
                postalCode = keyboard.nextInt();
                ok = true;
            } catch (Exception e){
                System.out.println("Only numbers allowed");
            }
        }
        
        return postalCode;
    }
    
    /**
     * The method takes the input of phone number of person.
     */
    private String inputPhone()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Write Phone Number:  ");
        String phone = keyboard.nextLine();
        return phone;
    } 
    
    /**
     * The method takes the input of email of person.
     */
    private String inputEmail()
    {
        boolean ok = false;
        String email = "";
        
        while (!ok) {
            try {
                // makes an object keyboard to read input from the console
                Scanner keyboard = new Scanner(System.in);
                System.out.print(" Write Email: ");
                email = keyboard.nextLine();
                ok = true;
            } catch (Exception e) {
                System.out.println("An error has occurred");
            }
        }
      
        return email;
    }
    
    /**
     * The method takes the input of customer type of person.
     */
    private String inputCustomerType()
    {
        boolean ok = false;
        String customerType = "";
        
        while (!ok) {
            try {
                // makes an object keyboard to read input from the console
                Scanner keyboard = new Scanner(System.in);
                System.out.print(" Write Customer Type: ");
                customerType = keyboard.nextLine();
                ok = true;
            } catch (Exception e) {
                System.out.println("An error has occurred");
            }
        }
       
        return customerType;
    }
    
    /**
     * The method takes the input of department of person.
     */
    private String inputDepartment()
    {
        boolean ok = false;
        String department = "";
        
        while (!ok) {
            try {
                // makes an object keyboard to read input from the console
                Scanner keyboard = new Scanner(System.in);
                System.out.print(" Write Department: ");
                department = keyboard.nextLine();
                ok = true;
            } catch (Exception e) {
                System.out.println("An error has occurred");
            }
        }
      
        return department;
    }
    
    /**
     * The method takes the input of product of person.
     */
    private String inputProduct() 
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Write Product: ");
        String product = keyboard.nextLine();
        return product;
    }
    
    /**
     * The method takes the input of amount to add to account balance of person.
     */
    private Double inputPersonNewBalance()
    {
        boolean ok = false;
        double addToBalance = 0.0;
        
        while (!ok) {
            try {
                // makes an object keyboard to read input from the console
                Scanner keyboard = new Scanner(System.in);
                System.out.print(" Write Amount to Add: ");
                addToBalance = keyboard.nextDouble();
                ok = true;
            } catch (Exception e) {
                System.out.println("Only numbers allowed");
            }
        }
        
        return addToBalance;
    }
    // end input methods
    
    /**
     * The method starts the add Person procedure.
     */
    private void startAddPerson()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" (1) Add Customer");
        System.out.println(" (2) Add Salesman");
        System.out.println(" (3) Add Supplier");
        int choise = keyboard.nextInt();
        
        if (choise == 1) {
            addCustomer();
        } else if (choise == 2) {
            addSalesman();
        } else if (choise == 3) {
            addSupplier();
        }
    }
    
    /**
     * The method adds a customer to the PersonColletion persons.
     */
    private void addCustomer()
    {
        String name = inputName();
        String address = inputAddress();
        String phoneNo = inputPhone();
        String email = inputEmail();
        int postalCode = inputPostalCode();
        String city = inputCity();
        String customerType = inputCustomerType();

        personCTR.addCustomer(name, address, phoneNo, email, customerType, city, postalCode);
    }
    
    /**
     * The method adds a salesman to the PersonColletion persons.
     */
    private void addSalesman()
    {
        String name = inputName();
        String address = inputAddress();
        String phoneNo = inputPhone();
        String email = inputEmail();
        int postalCode = inputPostalCode();
        String city = inputCity();
        String department = inputDepartment();

        personCTR.addSalesman(name, address, phoneNo, email, department, city, postalCode);
    }
    
    /**
     * The method adds a supplier to the PersonColletion persons.
     */
    private void addSupplier()
    {
        String name = inputName();
        String address = inputAddress();
        String phoneNo = inputPhone();
        String email = inputEmail();
        int postalCode = inputPostalCode();
        String city = inputCity();
        String product = inputProduct();

        personCTR.addSupplier(name, address, phoneNo, email, product, city, postalCode);
    }

    /**
     * The method finds a person from phone number.
     */
    private void startGetPerson()
    {
        String phone = inputPhone();
        
        Person person = null;
        person = personCTR.findPerson(phone);
        
        try {
            if (person != null) {
                System.out.print("\f");
                printPersonType(person);
                printPersonGeneral(person);
                
                System.out.println("\n Press the enter key to return to menu");
                Scanner sc = new Scanner(System.in);
                while(!sc.nextLine().equals(""));
                
            } else {
                
                System.out.println("\n ** No person with that phone number was found! **");
                Thread.sleep(1500);
                
            }
        }
        catch (Exception e)  {
            System.out.println(" ** An error has occurred! **");
        }
        
    }
    
    /**
     * The method deletes a person from phone number.
     */
    private void startDeletePerson() 
    {
        String phone = inputPhone();
        
        try {
            if(personCTR.deletePerson(phone)) {
                System.out.println("\n ** Person deleted! **");
                Thread.sleep(1500);
            }
            else {
                System.out.println("\n ** No person with that phone number was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)  {
            System.out.println(" ** An error has occurred! **");
        }
    }
    
    /**
     * The method lists all persons.
     */
    private void startListAllPersons() 
    {
        ArrayList<Person> personList = personCTR.listAllPersons();
        System.out.print("\f");
        
        for (Person person : personList) {
            printPersonType(person);
            printPersonGeneral(person);
        }
        
        Scanner sc = new Scanner(System.in);
        while(!sc.nextLine().equals(""));
    }
    
    /**
     * This method determines the instance of an object and depending on its instance type prints information about the type it is of.
     */
    private void printPersonType(Person person)
    {
        if (person instanceof Customer) {
            Customer newPerson = (Customer) person;
            System.out.println(" -- Customer Information --");
            System.out.println("Customer Type: " + newPerson.getCustomerType());
            System.out.println("Account Balance: " + newPerson.getAccountBalance() + " kr.");
        } else if (person instanceof Salesman) {
            Salesman newPerson = (Salesman) person;
            System.out.println(" -- Salesman Information --");
            System.out.println("Department: " + newPerson.getDepartment());
        } else {
            Supplier newPerson = (Supplier) person;
            System.out.println(" -- Supplier Information --");
            System.out.println(newPerson.getProduct());
        }
        System.out.println("");
    }
    
    /**
     * This method prints general person information.
     */
    private void printPersonGeneral(Person person) {
        System.out.println("Name: " + person.getName());
        System.out.println("Address :" + person.getAddress());
        System.out.println("City & Postal Code: " + person.getCity() + ", " + person.getPostalCode());
        System.out.println("Phone: " + person.getPhoneNo());
        System.out.println("Email: " + person.getEmail());
        System.out.println("");
    }
    
}